package com.ccpdown.util.xml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.R.string;

import com.ccpdown.vo.ConfiedText;
import com.ccpdown.vo.SoftList;
import com.ccpdown.vo.Software;

public class SAXHandler extends DefaultHandler {
	
	private String currentElement=null;	//标签名
	private String currentValue=null;	//标签值
	
	private ConfiedText ct;	//
	private List cts;	//
	
	private List list;
	private Software software;	
	private Software swDown;	
	private String acctributesType;
	private String currentPage;
	private SoftList softList;
	
	//外接口，获得各种数据;
	
	//获得类型列表;
	public List getCts() {
		return cts;
	}
	//
	public List getList(){
		return list;
	}
	//返回软件信息;
	public Software getSoftware(){
		return swDown;
	}
	
	public String getAcctributesType() {
		return acctributesType;
	}
	
	public String getCurrentPage(){
		return currentPage;
	}
	//返回软件列表；
	public SoftList getSoftList() {
		return softList;
	}

	//开始读取标签的数据
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		//super.startElement(uri, localName, qName, attributes);
		this.currentElement = localName;
		if (TagClass.SOFT_TYPE.equals(currentElement)) {
			ct = new ConfiedText();
		}
		
		if(TagClass.SOFT.equals(currentElement)){
			software=new Software();
		}
		
		if(TagClass.SOFTWARE.equals(localName)){
			swDown=new Software();
		}
		
		if(TagClass.SOFTWARES.equals(localName)){
			softList=new SoftList();
			for(int i=0;i<attributes.getLength();i++){
				if(attributes.getLocalName(i).equals(TagClass.TYPENAME)){
					this.softList.typeName=attributes.getValue(i);
				}
				if(attributes.getLocalName(i).equals(TagClass.PAGE)){
					this.softList.currentPage=Integer.parseInt(attributes.getValue(i)) ;
				}
				if(attributes.getLocalName(i).equals(TagClass.TPAGE)){
					this.softList.totalPage=Integer.parseInt(attributes.getValue(i)) ;
				}
				if(attributes.getLocalName(i).equals(TagClass.COUNTN)){
					this.softList.countNum=Integer.parseInt(attributes.getValue(i)) ;
				}
				if(TagClass.PARENTTYPE.equals(attributes.getLocalName(i))){
					this.softList.parentType=attributes.getValue(i);
				}
			}
		}
	}

	//取得标签内信息的数据
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		//解析类别列表
		if(TagClass.TYPE_NAME.equals(currentElement)){
			this.ct.typeName=new String(ch, start, length);
		}
		if(TagClass.TYPE_ID.equals(currentElement)){
			this.ct.typeId= new String(ch, start, length);
		}
		if(TagClass.TYPE_ICON.equals(currentElement)){
			this.ct.typeIcon=new String(ch,start,length);
		}
		if(TagClass.TYPE_COUNT.equals(currentElement)){
			this.ct.typeCount=Integer.parseInt( new String(ch,start,length));
		}
		//解析软件列表；
		if(TagClass.SOFT_NAME.equals(currentElement)){
			this.software.softName= new String(ch, start, length);
		}
		if(TagClass.SOFT_ID.equals(currentElement)){
			this.software.softId =new String(ch, start, length) ;
		}
		if(TagClass.SOFT_WRITER.equals(currentElement)){
			this.software.writer= new String(ch, start, length);
		}
		if(TagClass.SOFT_START.equals(currentElement)){
			this.software.softStart= Integer.parseInt(new String(ch, start, length));
		}
		if(TagClass.SOFT_SQ.equals(currentElement)){
			this.software.softSq=new String(ch, start, length) ;
		}
		if(TagClass.SOFT_ICON.equals(currentElement)){
			this.software.iconPath=new String(ch, start, length) ;
		}
		if(TagClass.SOFT_PACKAGE.equals(currentElement)){
			this.software.packagename=new String(ch, start, length) ;
		}
		if(TagClass.SOFT_VERSION.equals(currentElement)){
			this.software.version=new String(ch, start, length) ;
		}
		if(TagClass.SOFT_CODE.equals(currentElement)){
			this.software.versionCode=Integer.parseInt( new String(ch, start, length)) ;
		}
		//解析软件详细信息
		if(TagClass.SNAME.equals(currentElement)){
			this.swDown.softName=new String(ch, start, length) ;
		}
		if(TagClass.STYPE.equals(currentElement)){
			this.swDown.typeName=new String(ch, start, length) ;
		}
		if(TagClass.SICON.equals(currentElement)){
			this.swDown.softPic=new String(ch, start, length) ;
		}
		if(TagClass.SWRITER.equals(currentElement)){
			this.swDown.writer=new String(ch, start, length) ;
		}
		if(TagClass.SSAY.equals(currentElement)){
			this.swDown.softSay=new String(ch, start, length) ;
			System.out.println(swDown.softSay);
		}
		if(TagClass.SSTART.equals(currentElement)){
			this.swDown.softStart=Integer.parseInt(new String(ch, start, length)) ;
		}
		if(TagClass.SSIZE.equals(currentElement)){
			this.swDown.fileSize=new String(ch, start, length) ;
		}
		if(TagClass.SSQ.equals(currentElement)){
			this.swDown.softSq=new String(ch, start, length) ;
		}
		if(TagClass.SPATH.equals(currentElement)){
			this.swDown.pathStr=new String(ch, start, length) ;
		}
		if(TagClass.SPIC.equals(currentElement)){
			this.swDown.softPic=new String(ch, start, length) ;
		}		
		if(TagClass.STIME.equals(currentElement)){
			swDown.softTime=new String(ch, start, length);
		}
		if(TagClass.SVERSION.equals(currentElement)){
			swDown.version=new String(ch, start, length);
		}
		if(TagClass.SVERSIONCODE.equals(currentElement)){
			swDown.versionCode=Integer.getInteger( new String(ch, start, length));
		}
		
	}

	//读取结束标签的数据
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		//super.endElement(uri, localName, qName);
		if(localName.equals(TagClass.SOFT_TYPE)){
			if(cts==null){
				cts=new ArrayList<ConfiedText>();
			}
			cts.add(ct);
		}
		if(TagClass.SOFT.equals(localName)){
			if(list==null){
				list=new ArrayList();
			}
			list.add(software);
		}
		if(TagClass.SOFTWARES.equals(localName)){
			softList.sws=list;
		}
		this.currentElement=null;
		//this.currentValue=null;
	}
	
	
}
